package com.orchid.community.models.controller;

import cn.hutool.core.util.StrUtil;
import com.orchid.community.common.utils.CodeEnum;
import com.orchid.community.common.utils.MsgResponse;
import com.orchid.community.common.utils.ResultUtils;
import com.orchid.community.models.DTO.TagListRequestDTO;
import com.orchid.community.models.DTO.TagRequestDTO;
import com.orchid.community.models.service.TagsService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/tag")
public class TagsController {

    @Resource
    private TagsService tagsService;

    @PostMapping("/create")
    public MsgResponse createTag(@RequestBody TagRequestDTO request) {
        if (request == null || StrUtil.isEmpty(request.getName()) || request.getKind() == null) {
            return ResultUtils.fail(CodeEnum.DATA_ERROR);
        }

        return tagsService.createTag(request);
    }


    @PostMapping("/delete")
    public MsgResponse deleteTag(@RequestBody TagRequestDTO request) {
        if (request == null || request.getId() == null) {
            return ResultUtils.fail(CodeEnum.DATA_ERROR);
        }
        return tagsService.deleteTag(request);
    }

    @PostMapping("/update")
    private MsgResponse updateTag (@RequestBody TagRequestDTO request) {
        if (request == null || request.getId() == null || StrUtil.isEmpty(request.getName())) {
            return ResultUtils.fail(CodeEnum.DATA_ERROR);
        }
        return  tagsService.updateTag(request);
    }

    @PostMapping("/setStatus")
    public MsgResponse setStatus (@RequestBody TagRequestDTO request) {
        if (request == null || request.getId() == null || request.getStatus() == null) {
            return ResultUtils.fail(CodeEnum.DATA_ERROR);
        }
        return tagsService.setStatus(request);
    }

    @PostMapping("/getTagsList")
    public MsgResponse getTagsList(@RequestBody TagListRequestDTO tagListRequestDTO) {
        return tagsService.getPageList(tagListRequestDTO);

    }


}
